//
//  CZRetweetStatusView.swift
//  微博
//
//  Created by 霸气的刘吉庆 on 16/4/20.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
import YYText
//转发微博的view
class CZRetweetStatusView: UIView {
    
    //设置底部的约束,是否根据有配图去跟新
    var bottomCons: Constraint?
    //定义一个属性
    var statusViewModel: CZStatusViewModel? {
        
        didSet{
            
            //设置转发微博的内容
            
//            contentLabel.text = statusViewModel?.retweetText
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            bottomCons?.uninstall()
            //废弃底部的越是
            //设置配图数据,如果有数据,更新底部的约束,没有就不需要更新
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0 {
                
                //判断如果是第一张图片,设置itemSize为视图的大小
//                if pic_urls.count == 1{
//                    
//                    
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = statusViewModel!.retweetViewSize
//                    
//                    
//                }else {
//                    
//                    
//                    
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: CZPictureViewItenWH, height: CZPictureViewItenWH)
//                }
//                
                
                
                
                
                
                
                //给配图的view设置大小
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo(statusViewModel!.retweetViewSize)
//                    
//
//                })
//                //给配图赋值
//                pictureView.pic_urls = pic_urls
                
//                print(pic_urls)
//将配图显示出来
                
                //给视图数据和宽高
                pictureView.dataInfo = (pic_urls,statusViewModel!.retweetViewSize)
                
                
                pictureView.hidden = false
                //设置底部的约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                   bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(CZHomeCellMargin).constraint
                })
                
                
            }else{
                
                //代表没有配图
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                  bottomCons =  make.bottom.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin).constraint
                    
                })
                
                
                
            }
            
            
        }
        
        
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 240/255, alpha: 1)
        
        
        //设置他的内容
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(CZHomeCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin)
            make.leading.equalTo(contentLabel)
            make.size.equalTo(CGSizeMake(100, 100))
        }

//设置self的约束
        
        self.snp_makeConstraints { (make) -> Void in

            bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(CZHomeCellMargin).constraint
//            make.bottom.equalTo(contentLabel.snp_bottom).offset(CZHomeCellMargin)
            
        }

        
    }
  
    
    private lazy var contentLabel: YYLabel = {
//       UILabel(textColor: UIColor.darkGrayColor(), fontSize: CZHomeCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * CZHomeCellMargin)
        let label = YYLabel()
        label.userInteractionEnabled = true
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CZHomeCellContentFontSize)
        label.preferredMaxLayoutWidth = SCREENW - 2 * CZHomeCellMargin
        //        label.backgroundColor = RandomColor
        label.numberOfLines = 0
        return label
        
    }()
    
    //注意这个是flowLayout
    private lazy var pictureView: CZPictureView = CZPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
